package com.runnirr.doodles;

import java.util.ArrayList;
import java.util.List;

import com.runnirr.doodles.R;
import com.runnirr.doodles.AsyncOperations.LoadDoodlesAsync;
import com.runnirr.doodles.Helpers.DoodleData;
import com.runnirr.doodles.Helpers.Utils;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class DoodleList extends ListActivity {

	private List<String> listItems = new ArrayList<String>();
	private ArrayAdapter<String> adapter;
	private List<DoodleData> dataList;
	private int year;
	private int month;


	public DoodleList() {
	}

	/** Called with the activity is first created. */
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Inflate our UI from its XML layout description.
		setContentView(R.layout.doodle_list_layout);

		if (Utils.isApi11Plus()){
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		
		// map the displayed list with our list in memory
		adapter=new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1,
				listItems);
		setListAdapter(adapter);

		Intent i = getIntent();
		year = i.getIntExtra("year", 2000);
		month = i.getIntExtra("month", 1);
		
		setTitle(Utils.getMonthNameFromInt(month) + " " + year);
		
		new LoadDoodlesAsync().execute(this);
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case android.R.id.home:
	            // This is called when the Home (Up) button is pressed
	            // in the Action Bar.
	            Intent parentActivityIntent = new Intent(this, DoodleFilter.class);
	            parentActivityIntent.addFlags(
	                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
	                    Intent.FLAG_ACTIVITY_NEW_TASK);
	            parentActivityIntent.putExtra("year", year);
	            parentActivityIntent.putExtra("month", month - 1);		// Convert from 1 base to 0 base
	            startActivity(parentActivityIntent);
	            finish();
	            return true;
	    }
	    return super.onOptionsItemSelected(item);
	}
	
	

	private void addDoodleEventListeners(){
		final ListView lv = (ListView) findViewById(android.R.id.list);
		lv.setOnItemClickListener(listItemClick);
	}


	OnItemClickListener listItemClick = new OnItemClickListener(){
		public void onItemClick(AdapterView<?> a, View v, int position, long id) {
			DoodleData dd = dataList.get(position);

			Intent intent = new Intent(DoodleList.this, DoodleDetails.class);
			
			intent.putExtra("doodle", dd);

			startActivity(intent);
		}

	};

	private void addListToUI(){
		for (DoodleData dd : dataList){
			listItems.add(dd.title);
		}
		adapter.notifyDataSetChanged();
	}

	/**
	 * Called when the activity is about to start interacting with the user.
	 */
	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * Called when your activity's options menu needs to be created.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		return true;
	}

	/**
	 * Called right before your activity's option menu is displayed.
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		return true;
	}


	public void onLoadDoodlesComplete(List<DoodleData> result) {
		dataList = result;
		addDoodleEventListeners();
		findViewById(R.id.LoadingIndicator).setVisibility(View.GONE);
		addListToUI();
		if(result.size() <= 0){
			findViewById(R.id.NoDoodlesLabel).setVisibility(View.VISIBLE);
		}
		
	}

}
